﻿namespace Basic.Cluster.Manager
{
    public class ClusterConfig
    {
        public string ClusterManagerProxy { get; set; } = "Consul";

        public string ClusterManagerAddress { get; set; }

        public string AccessorID { get; set; }

        public string SecretID { get; set; }

        public int? HealthInterval { get; set; } = 15;

        public int? HealthTimeout { get; set; } = 5;

        public string[] Keys { get; set; }

        /// <summary>
        /// 配置检查间隔, 默认180（秒）
        /// </summary>
        public int ConfigCheckInterval { get; set; } = 180;

        #region 

        /// <summary>
        /// 服务注册表
        /// </summary>
        public IDictionary<string, string> ServiceMeta { get; set; }

        public string this[string key]
        {
            get => ServiceMeta?.TryGetValue(key, out string val) == true ? val : null;
            set => (ServiceMeta ??= new Dictionary<string, string>())[key] = value;
        }

        public string ID => $"{Name}-{IP}-{Port}";

        public string Name => this["Name"];

        public string IP => this["IP"];

        public int Port => int.TryParse(this["Port"], out int port) ? port : 0;

        public string HealthCheck => this["HealthCheck"];

        public string Token => this["Token"];

        #endregion

    }
}